package com.luoran.bartalks.action;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.luoran.bartalks.utils.ImagesUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author liang ningning
 * @date ${date}
 */
public abstract class AbstractImageAction {

	protected String imagePath;

	public abstract String getImagePath(HttpServletRequest request);

	private String initImagePath(HttpServletRequest request) {
		if (StringUtils.isBlank(this.imagePath)) {
			this.imagePath = getImagePath(request);
		}
		return this.imagePath;
	}

	@RequestMapping("/upload-image")
	@ResponseBody
	public String uploadImage(MultipartHttpServletRequest req, HttpServletRequest request) {
		return ImagesUtil.getImageUrlByLast(req, initImagePath(request));
	}

	@RequestMapping("/image/{fileName}")
	public void getImage(@PathVariable String fileName, HttpServletResponse res, HttpServletRequest request) {
		ImagesUtil.getImage(res, fileName, initImagePath(request));
	}

}
